<template>
	<!-- 新增/修改编辑框 -->
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" draggable>
		<el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" :label-width="100">
			<el-form-item label="头像" prop="head">
				<!-- <el-input v-model="dataForm.head" placeholder="头像"></el-input> -->
				<img :src="dataForm.head" alt="头像" style="width: 50px; height: 50px; border-radius: 50%" />
			</el-form-item>
			<el-form-item label="昵称" prop="name">
				<el-input v-model="dataForm.name" placeholder="昵称"></el-input>
			</el-form-item>
			<el-form-item label="游戏名" prop="gamename">
				<el-input v-model="dataForm.gamename" placeholder="游戏名"></el-input>
			</el-form-item>
			<el-form-item label="标题" prop="title">
				<el-input v-model="dataForm.title" placeholder="标题"></el-input>
			</el-form-item>
			<el-form-item label="内容" prop="content">
				<el-input v-model="dataForm.content" placeholder="内容"></el-input>
			</el-form-item>
			<!-- <el-form-item label="图片列表" prop="image">
				<el-input v-model="dataForm.image" placeholder="图片列表"></el-input>
				<div style="margin-top: 8px">
					<el-button v-if="dataForm.image" type="primary" link @click="handleShowImageViewer()"> 查看图片 </el-button>
					<span v-else>无图片</span>
				</div>
			</el-form-item> -->

			<el-form-item label="图片列表" prop="image">
				<!-- <el-input v-model="dataForm.image" placeholder="图片列表"></el-input> -->
				<div style="margin-top: 8px">
					<template v-if="dataForm.image">
						<div v-for="(img, index) in dataForm.image.split(',').filter(img => img && img.trim() !== '')"
							:key="index" style="display: inline-block; margin-right: 10px;">
							<img :src="img" alt="图片"
								style="width: 50px; height: 50px; object-fit: cover; border-radius: 4px;" />
						</div>
						<el-button type="primary" link @click="handleShowImageViewer()"> 查看图片 </el-button>
					</template>
					<span v-else>无图片</span>
				</div>
			</el-form-item>
			<el-form-item label="标签1" prop="tag1">
				<el-input v-model="dataForm.tag1" placeholder="标签1"></el-input>
			</el-form-item>
			<el-form-item label="标签2" prop="tag2">
				<el-input v-model="dataForm.tag2" placeholder="标签2"></el-input>
			</el-form-item>
			<el-form-item label="标签3" prop="tag3">
				<el-input v-model="dataForm.tag3" placeholder="标签3"></el-input>
			</el-form-item>
			<el-form-item label="类型/1热游/2新游/3好游/4烂游" prop="type">
				<el-input v-model="dataForm.type" placeholder="类型/1热游/2新游/3好游/4烂游"></el-input>
			</el-form-item>
			<el-form-item label="浏览量" prop="num1">
				<el-input v-model="dataForm.num1" placeholder="浏览量"></el-input>
			</el-form-item>
			<el-form-item label="评论量" prop="num2">
				<el-input v-model="dataForm.num2" placeholder="评论量"></el-input>
			</el-form-item>
			<el-form-item label="点赞量" prop="num3">
				<el-input v-model="dataForm.num3" placeholder="点赞量"></el-input>
			</el-form-item>
			<el-form-item label="" prop="openid">
				<el-input v-model="dataForm.openid" placeholder=""></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="submitHandle()">确定</el-button>
		</template>
	</el-dialog>
	<el-image-viewer v-if="imageViewerVisible" :url-list="imageUrlList" :initial-index="0"
		@close="imageViewerVisible = false" :teleported="true" :append-to-body="true" :z-index="9999" />
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import { useForumApi, useForumSubmitApi } from '@/api/kyy/forum'
import { ElImageViewer } from 'element-plus'

const emit = defineEmits(['refreshDataList'])

const visible = defineModel<boolean>('visible')
const dataFormRef = ref()

const dataForm = reactive({
	id: '',
	head: '',
	name: '',
	gamename: '',
	title: '',
	content: '',
	image: '',
	tag1: '',
	tag2: '',
	tag3: '',
	type: '',
	num1: '',
	num2: '',
	num3: '',
	version: '',
	deleted: '',
	creator: '',
	createTime: '',
	updater: '',
	updateTime: '',
	openid: ''
})

const imageViewerVisible = ref(false)
const imageUrlList = ref<string[]>([])

const init = (id?: number) => {
	if (id) {
		getForum(id)
	}
}

const getForum = (id: number) => {
	useForumApi(id).then(res => {
		Object.assign(dataForm, res.data)
	})
}

const dataRules = ref({
	head: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	gamename: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	title: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	content: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	image: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	tag1: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	tag2: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	tag3: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	type: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	num1: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	num2: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	num3: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
	openid: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
})

// 表单提交
const submitHandle = () => {
	dataFormRef.value.validate(async (valid: boolean) => {
		if (!valid) {
			return false
		}

		useForumSubmitApi(dataForm).then(() => {
			ElMessage.success({
				message: '操作成功',
				duration: 500,
				onClose: () => {
					visible.value = false
					emit('refreshDataList')
				}
			})
		})
	})
}

const handleShowImageViewer = () => {
	if (dataForm.image) {
		imageUrlList.value = dataForm.image.split(',').filter(img => img && img.trim() !== '')
		if (imageUrlList.value.length > 0) {
			setTimeout(() => {
				imageViewerVisible.value = true
			}, 0)
		}
	}
}

defineExpose({
	init
})
</script>
